﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class PdfScanInstruction:InstructionBase
    {
        #region props
        private string _FileListInputXmlFilePath;
        /// <summary>
        /// <Files><File DocID="0" FilePath="" /></Files>
        /// </summary>
        public string FileListInputXmlFilePath
        {
            get
            {
                return this._FileListInputXmlFilePath;
            }
            set
            {
                this._FileListInputXmlFilePath = value;
            }
        }

        private string _Pattern;
        /// <summary></summary>
        public string Pattern
        {
            get
            {
                return this._Pattern;
            }
            set
            {
                this._Pattern = value;
            }
        }

        private double _MatchScoreThreshold;
        /// <summary></summary>
        public double MatchScoreThreshold
        {
            get
            {
                return this._MatchScoreThreshold;
            }
            set
            {
                this._MatchScoreThreshold = value;
            }
        }

        private int _ThreadPoolSize;
        /// <summary></summary>
        public int ThreadPoolSize
        {
            get
            {
                return this._ThreadPoolSize;
            }
            set
            {
                this._ThreadPoolSize = value;
            }
        }

        private string _LogFilePath;
        /// <summary></summary>
        public string LogFilePath
        {
            get
            {
                return this._LogFilePath;
            }
            set
            {
                this._LogFilePath = value;
            }
        }

        private string _ErrorFilePath;
        /// <summary></summary>
        public string ErrorFilePath
        {
            get
            {
                return this._ErrorFilePath;
            }
            set
            {
                this._ErrorFilePath = value;
            }
        }

        private string _ResultFilePath;
        /// <summary>
        /// <Files><File DocID="1" Pages="1,2,3" IsElectronic="False" IsScanned="False" /></Files>
        /// </summary>
        public string ResultFilePath
        {
            get
            {
                return this._ResultFilePath;
            }
            set
            {
                this._ResultFilePath = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public PdfScanInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._FileListInputXmlFilePath = string.Empty;
            this._Pattern = string.Empty;
            this._MatchScoreThreshold = 0.8;
            this._ThreadPoolSize = 10;
            this._LogFilePath = string.Empty;
            this._ErrorFilePath = string.Empty;
            this._ResultFilePath = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="fileListXmlFilePath"></param>
        /// <param name="pattern"></param>
        /// <param name="minScore"></param>
        /// <param name="poolSize"></param>
        /// <param name="logFilePath"></param>
        /// <param name="errFilePath"></param>
        /// <param name="resultFilePath"></param>
        public PdfScanInstruction(Guid componentID, Guid jobTicketID,
            string fileListXmlFilePath,
            string pattern,
            double minScore,
            int poolSize,
            string logFilePath,
            string errFilePath,
            string resultFilePath) 
            : base(componentID, jobTicketID)
        {
            this._FileListInputXmlFilePath = fileListXmlFilePath;
            this._Pattern = pattern;
            this._MatchScoreThreshold = minScore;
            this._ThreadPoolSize = poolSize;
            this._LogFilePath = logFilePath;
            this._ErrorFilePath = errFilePath;
            this._ResultFilePath = resultFilePath;
        }
        #endregion

        /// <summary>
        /// instantiated from xml
        /// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
        /// instantiate from first child of root node
        /// </summary>
        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                PdfScanInstruction instruction = new PdfScanInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.FileListInputXmlFilePath = XmlDataUtil.GetAttributeValue(xNode, "FileListInputXmlFilePath", "");
                instruction.Pattern = XmlDataUtil.GetAttributeValue(xNode, "Pattern", "");
                instruction.MatchScoreThreshold =
                    double.Parse(XmlDataUtil.GetAttributeValue(xNode, "MatchScoreThreshold", "0.8"));
                instruction.ThreadPoolSize = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "ThreadPoolSize", "10"));
                instruction.LogFilePath = XmlDataUtil.GetAttributeValue(xNode, "LogFilePath", "");
                instruction.ErrorFilePath = XmlDataUtil.GetAttributeValue(xNode, "ErrFilePath", "");
                instruction.ResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "ResultFilePath", "");

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }

        /// <summary>serialize settings to xml</summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileListInputXmlFilePath", this.FileListInputXmlFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Pattern",this.Pattern);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "MatchScoreThreshold", this.MatchScoreThreshold.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ThreadPoolSize", this.ThreadPoolSize.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LogFilePath", this.LogFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ErrFilePath", this.ErrorFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ResultFilePath",this.ResultFilePath);
            
            return xNode;
        }
    }
}
